package com.itm.lqm.dao;

import java.io.Serializable;


import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;


import com.itm.lqm.entity.Student;
import com.itm.lqm.util.HibernateUtil;
public class StudentDao {
	//持久化层，对数据库的增删改查
	// 查询所有学生
	public List<Student> findAllStudent() {
		Session session = HibernateUtil.openSession();
		Transaction tran = session.beginTransaction();
		
		Query query = session.createQuery("from Student");
		List<Student> list = query.list();

		for (Student em : list) {
			System.out.println("dao" + em);
		}
		return list;

		// TODO Auto-generated method stub

	}

	// 按班级查询
	public List<Student> findStudentByClas(String clas) {

		Session session = HibernateUtil.openSession();
		Transaction tran = session.beginTransaction();

		Query query = session.createQuery("from Student where clas=?");
		query.setString(0, clas);

		List<Student> list = query.list();
		for (Student em : list) {

			System.out.println(em);
		}
		return list;
	}

	// 按照sid查询学生
	public Student findStudentBySid(int sid) {

		Session session = HibernateUtil.openSession();
		Transaction tran = session.beginTransaction();

		Query query = session.createQuery("from Student where sid=?");
//		query.setString(0, sno);
		query.setInteger(0, sid);

		Student student =(Student) query.uniqueResult();
		
		return student;
	}

	/*
	 * 验证该学生是否存在<根据sid>
	 */
	public boolean isExistSid(int sid) {
		return findStudentBySid(sid) == null ? false : true;
	}

	// 添加学生
	public boolean addStudent(Student s) {
		Session session = null;
		Transaction tran = null;
		try {
			session = HibernateUtil.openSession();
			tran = session.beginTransaction();
			// ---------------------------------
			// 查找Student对象
			//Student ss = new Student();
			session.save(s);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		} finally {
			if (null != session) {
				session.close();
			}
		}
		return false;
	}
	//删除学生
	public boolean deleteStudent(int sno) {
		Session session = null;
		Transaction tran = null;
		try {
			session = HibernateUtil.openSession();
			tran = session.beginTransaction();
			// ---------------------------------

			// 查找Student对象
			Student s = session.load(Student.class,sno);
			//Student s = new Student ();
			session.delete(s);
			// ---------------------------------
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		} finally {
			if (null != session) {
				session.close();
			}
		}
		return false;
	}
	// 修改学生
	public  boolean updateStudent(Student s) {
		Session session = null;
		Transaction tran = null;
		try {
			session = HibernateUtil.openSession();
			tran = session.beginTransaction();
			// ---------------------------------

			// 查找Student对象
			
			Query query = session.createQuery("update Student set sno=?,sname=?, clas=? ,sex=?, major=? where sid=?");
			query.setParameter(0,s.getSno());
			query.setParameter(1,s.getSname());
			
			query.setParameter(2,s.getClas());
			query.setParameter(3,s.getSex());
			query.setParameter(4,s.getMajor());
			query.setParameter(5,s.getSid());
			return query.executeUpdate()>0;
			// ---------------------------------
			//tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			//tran.rollback();
		} finally {
			if (null != session) {
				session.close();
			}
		}
		return false;
	}

}
